package com.zk.viewcodes.ui;

import android.app.Dialog;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.util.Log;
import android.view.Display;
import android.view.View;

import com.zk.viewcodes.R;

/**
 * author: ZK.
 * date:   On 2018-12-04.
 */
public class MBottomSheetDialogFragment extends BottomSheetDialogFragment {


    private BottomSheetBehavior mBehavior;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return getBottomSheetDialog(savedInstanceState);
    }


    public BottomSheetDialog getBottomSheetDialog(Bundle savedInstanceState) {
        BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
        View view = View.inflate(getContext(), R.layout.layout_bottom_sheet_dialog, null);
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        /*ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(point.x, point.y * 2 / 3); //设置最大高度
        view.setLayoutParams(layoutParams);*/
        bottomSheetDialog.setContentView(view);
        mBehavior = BottomSheetBehavior.from((View) view.getParent());
        mBehavior.setPeekHeight(point.y * 1 / 10); //设置折叠状态时的高度
        mBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {

            /**
             * @param bottomSheet
             * @param newState  STATE_DRAGGING = 1    : 拖曳
             *                  STATE_SETTLING = 2    : 固定
             *                  STATE_EXPANDED = 3    : 完全展开
             *                  STATE_COLLAPSED = 4   ：折叠
             *                  STATE_HIDDEN = 5      ：隐藏
             */
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                switch (newState) {
                    case BottomSheetBehavior.STATE_DRAGGING:
                        Log.d("=======onStateChanged", "拖曳");
                        break;
                    case BottomSheetBehavior.STATE_EXPANDED:
                        Log.d("=======onStateChanged", "完全展开");
                        break;
                    case BottomSheetBehavior.STATE_COLLAPSED:
                        Log.d("=======onStateChanged", "折叠");
                        break;
                    case BottomSheetBehavior.STATE_HIDDEN:
                        dismiss();
                        break;
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                Log.d("=======onSlide", slideOffset + "");
            }
        });
        return bottomSheetDialog;
    }


    @Override
    public void onStart()
    {
        super.onStart();
        mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);//全屏展开
    }
}
